package io.framework.modules.noise.service.impl;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.framework.common.page.PageData;
import io.framework.common.service.impl.CrudServiceImpl;
import io.framework.modules.noise.dao.NoiseDayDao;
import io.framework.modules.noise.dto.NoiseDayDTO;
import io.framework.modules.noise.entity.NoiseDay;
import io.framework.modules.noise.service.NoiseDayService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * 噪音 环境质量监测
 *
 * @author Gujq email
 * @since 1.0.0 2020-06-01
 */
@Service
public class NoiseDayServiceImpl extends CrudServiceImpl<NoiseDayDao, NoiseDay, NoiseDayDTO> implements NoiseDayService {

    @Autowired
    private  NoiseDayDao noiseDayDao;

    @Override
    public QueryWrapper<NoiseDay> getWrapper(Map<String, Object> params) {
        return null;
    }



    @Override
    public NoiseDayDTO getNoiseRecentTime() {
        return noiseDayDao.getNoiseRecentTime();
    }


    @Override
    public PageData<NoiseDayDTO> pageList(Map<String, Object> params) {
        JSONObject jsonObject = JSONObject.parseObject(JSON.toJSONString(params));
        String orderField = jsonObject.getString("orderField");
        String order = jsonObject.getString("order");
        String time = jsonObject.getString("time");
        Integer page = jsonObject.getInteger("page");
        Integer limit = jsonObject.getInteger("limit");
        QueryWrapper<NoiseDay> queryWrapper = new QueryWrapper<>();
//        queryWrapper.select("").likeLeft()


        if (StringUtils.isNotBlank(time)) {
            queryWrapper.eq("date_format(rec_time,'%Y')", time);
        }
        Page<NoiseDay> p = new Page<>(page, limit);
        if (StringUtils.isNotBlank(orderField) && StringUtils.isNotBlank(order)) {
            if (order.equals("desc")) {
                queryWrapper.orderByDesc(orderField);
            } else {
                queryWrapper.orderByAsc(orderField);
            }
        }
        IPage<NoiseDay> ip = baseDao.selectPage(p, queryWrapper);
        List<NoiseDayDTO> dtoList = JSONArray.parseArray(JSONArray.toJSONString(ip.getRecords()), NoiseDayDTO.class);
        List<Map<String,Object>> bzList = noiseDayDao.SelectBz();
        //测点昼夜简的标准
        Map<String, String> zyBzMap = bzList.stream().collect(Collectors.toMap(e -> (e.get("CDDM") + "" + e.get("FLLX")), e -> e.get("BZSX") + "", (existing, replacement) -> replacement));
        //测点的功能区类别
        Map<String, String> gnqlbMap = bzList.stream().collect(Collectors.toMap(e -> e.get("CDDM") + "", e -> e.get("GNQLB") + "", (existing, replacement) -> replacement));
        for (NoiseDayDTO noiseDayDTO : dtoList) {
            String glqlb = gnqlbMap.get(noiseDayDTO.getPointId());
            noiseDayDTO.setGnqlb(glqlb);
            //昼间标准
            String zjbz = zyBzMap.get(noiseDayDTO.getPointId() + "1");
            noiseDayDTO.setZjbz(Float.valueOf(zjbz));
            //夜间标准
            String yjbz = zyBzMap.get(noiseDayDTO.getPointId() + "2");
            noiseDayDTO.setYjbz(Float.valueOf(yjbz));
            //昼间均值
            float ld = noiseDayDTO.getLd();
            if (Float.valueOf(zjbz)<ld){
                noiseDayDTO.setZjsfcb(1);
            }else {
                noiseDayDTO.setZjsfcb(0);
            }
            //夜间均值
            float ln = noiseDayDTO.getLn();
            if (Float.valueOf(yjbz)<ln){
                noiseDayDTO.setYjsfcb(1);
            }else {
                noiseDayDTO.setYjsfcb(0);
            }
        }
        return new PageData<NoiseDayDTO>(dtoList, ip.getTotal());
    }
}
